package com.wtwd.device.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wtwd.device.constant.Constants;
import com.wtwd.device.model.entity.SysUser;
import com.wtwd.device.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author mjy
 * @date 2021/06/18
 * @description
 */
@Component
public class GetCurrentSysUserInfoUtils {

    @Autowired
    public RedisService redisService;

    public SysUser getCurrentSysUserInfo(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader(Constants.HEADER_TOKEN_KEY);
        Map<String, Object> userMsg = redisService.getUserInfoByToken(token);
        String userString= (String)userMsg.get("user");
        JSONObject userJson = JSON.parseObject(userString);
        SysUser sysUser = userJson.toJavaObject(SysUser.class);
        return sysUser;
    }
}
